package com.example.weathermvvm.ui.weather

import android.graphics.Color
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.viewModels
import androidx.core.view.GravityCompat
import com.example.weathermvvm.R
import com.example.weathermvvm.databinding.ActivityWeatherBinding
import com.example.weathermvvm.util.InjectorUtil

class WeatherActivity : AppCompatActivity() {

    val viewModel:WeatherViewModel by viewModels {
        InjectorUtil.getWeatherModelFactory()
    }

    private lateinit var binding:ActivityWeatherBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e(localClassName,"onCreate...")
        binding = ActivityWeatherBinding.inflate(layoutInflater)
        setContentView(binding.root)
        if (Build.VERSION.SDK_INT >= 21){
            val decorView = window.decorView
            decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            window.statusBarColor = Color.TRANSPARENT
        }
        binding.viewModel = viewModel
        binding.resId = R.color.colorPrimary
        binding.lifecycleOwner = this
        viewModel.weatherId = if (viewModel.isWeatherCached()){
            viewModel.getCachedWeather().basic.weatherId
        }else {
            intent.getStringExtra("weather_id")
        }.toString()
        Log.e("WeatherActivity","${viewModel.weatherId.toString()}")
        binding.title.navButton.setOnClickListener {
            binding.drawerLayout.openDrawer(GravityCompat.START)
        }
        viewModel.getWeatherInfo()
    }
}